import os.path

from ui import Ui_Form
import sys
from PyQt5.QtWidgets import *
from PyQt5.Qt import *
from PyQt5.QtCore import *
from PyQt5 import Qt
from path import Path
from child_window.new_tyep import *
from child_window.new_xiangmu import *
from child_window import path_xiangmu
import re

rpath=Path()
class Logic(Ui_Form,QFrame):
    def __init__(self):
        super(Logic, self).__init__()
        self.setupUi(self)

        self.xaingmushu.installEventFilter(self)  # 监视对象tree，然后执行
        self.setContextMenuPolicy(Qt.CustomContextMenu)  # 激活右键策略
        self.customContextMenuRequested.connect(self.right_click)  # 将右键信号传递给槽函数
        self.setting = QSettings('../setting.ini', QSettings.IniFormat)
        if self.setting.value('path')=="":
            self.path_label.setText("项目管理路径")
        else:
            self.path_label.setText(self.setting.value('path'))
        self.show1.clicked.connect(self.Navigation_Bar_show)
        self.table_waibu.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)#自适应
        self.table_waibu.horizontalHeader().setHighlightSections(False)#塌陷
        self.table_waibu.verticalHeader().setVisible(False)#隐藏行
        self.table_waibu.horizontalHeader().setStyleSheet("QHeaderView::section{background-color:rgb(33, 37, 43);color:rgb(255,255,255)}")
        self.xaingmushu.header().setStyleSheet("QHeaderView::section{background-color:rgb(33, 37, 43);color:rgb(255,255,255)}")
        self.btn()

        self.pushButton_2.clicked.connect(self.setup_animate)
        self.pushButton_4.clicked.connect(self.setup_animate)
        self.btn_path.clicked.connect(self.path_project)

    def path_project(self):
        """项目路径界面"""
        self.project_path=path_xiangmu.Ui_Form()
        # self.project_path.setWindowFlag(Qt.WindowStaysOnTopHint)
        self.project_path.show()
        self.project_path.lineEdit.setText(rpath.root_path())
        self.project_path.btn_file.clicked.connect(self.FileDialog)
        self.project_path.singl.connect(self.label_xainshi)

    def label_xainshi(self,name):
        self.project_path.close()
        path_value = re.sub("\\\\", '/', name)


        self.setting.setValue('path',path_value)
        self.path_label.setText(name)
    def FileDialog(self):
        """操作文件管理器"""
        self.file=QFileDialog.getExistingDirectory(self,"请选择文件",rpath.root_path())
        self.project_path.lineEdit.setText(self.file)
        self.project_path.activateWindow()
        self.project_path.lineEdit.setFocus()

    def setup_animate(self):
        """设置动画展示"""
        if self.widget_shezhi.width()==0:
            w1=190
        else:
            w1=0

        self.shezhi=QPropertyAnimation(self.widget_shezhi,b'minimumWidth')
        self.shezhi.setDuration(500)
        self.shezhi.setStartValue(self.widget_shezhi.width())
        self.shezhi.setEndValue(w1)
        self.shezhi.start()

    def new_project_run(self):
        """启动新建项目窗口"""
        if self.setting.value('path')!="":
            self.project=new_project()
            self.project.setWindowFlag(Qt.WindowStaysOnTopHint)
            self.project.show()
            self.project.singl.connect(self.new_project)
        else:
            QMessageBox().information(self, "错误", "请先在设置中创建项目路径", QMessageBox.Yes)

    def new_project(self,project_name):
        """新建项目窗口"""
        self.project.close()
        try:
            os.makedirs(os.path.join(self.setting.value("path"), project_name))
            item=QTreeWidgetItem(self.xaingmushu)
            self.xaingmushu.setCurrentItem(item)
            item.setText(0,project_name)
        except FileExistsError:
            QMessageBox().information(self, "创建错误", "文件已经存在", QMessageBox.Yes)
    def right_click(self):
        self.mun=QMenu()
        self.mun.addAction(QIcon(os.path.join(rpath.root_path(),"image\\运行.png")),"运行")
        self.mun.move(QCursor.pos())
        self.mun.setStyleSheet("QMenu{background-color:rgb(33, 37, 43);color:rgb(255,255,255)}QMenu::item:selected{background-color:rgb(59,59,72);}")
        self.mun.show()
    def eventFilter(self, object, event):
        if event.type()==QEvent.ContextMenu:
            self.right_click()
            self.add=self.mun.addAction(QIcon(os.path.join(rpath.root_path(),"image\\加.png")),'增加')
            self.dele_node=self.mun.addAction(QIcon(os.path.join(rpath.root_path(),"image\\减少.png")),'删除')
            self.name=self.dele=self.mun.addAction('重命名')
            if self.xaingmushu.currentIndex().row()==-1:
                self.add.triggered.connect(self.new_project_run)
            else:
                try:
                    if self.xaingmushu.currentItem().parent().text(0)!="":
                        self.add.triggered.connect(self.new_project_run)
                        self.dele_node.triggered.connect(self.delte_child)

                except:
                    self.add.triggered.connect(self.run_tree_child)
                    self.dele_node.triggered.connect(self.del_top)
        return True
    def del_top(self):
        """删除顶级节点"""
        try:
            os.rmdir(os.path.join(self.setting.value('path'), self.xaingmushu.currentItem().text(0)))
            self.xaingmushu.takeTopLevelItem(self.xaingmushu.currentIndex().row())
        except OSError:
            QMessageBox().information(self, "错误", "当前项目下还存在其他文件,不能删除", QMessageBox.Yes)
    def delte_child(self):
        """删除子节点"""

        os.rmdir(os.path.join(self.setting.value("path"),self.xaingmushu.currentItem().parent().text(0)+"/"+self.xaingmushu.currentItem().text(0)))
        self.xaingmushu.currentItem().parent().takeChild(self.xaingmushu.currentIndex().row())
    def run_tree_child(self):
        """启动子节点页面"""
        self.child=new_type()
        self.child.show()
        self.child.singl.connect(self.new_tree_child)
    def new_tree_child(self,child_name):
        """新增子节点"""
        self.child.close()
        item =QTreeWidgetItem()
        self.xaingmushu.topLevelItem(self.xaingmushu.currentIndex().row()).addChild(item)
        self.xaingmushu.setCurrentItem(item)
        item.setCheckState(0,Qt.Unchecked)
        item.setText(0,child_name)

        os.makedirs(os.path.join(self.setting.value("path"),self.xaingmushu.currentItem().parent().text(0)+"/"+item.text(0)))
    def btn(self):
        self.btn_home.clicked.connect(self.btn_sender)
        self.test_case.clicked.connect(self.btn_sender)
    def btn_sender(self):
        index={
            'btn_home':1,
            'test_case':0
        }
        self.stackedWidget.setCurrentIndex(index[self.sender().objectName()])
    def Navigation_Bar_show(self):
        if self.zuobianlan.width()==66:
            w2=200
        else:
            w2=66
        self.pa=QPropertyAnimation(self.zuobianlan,b"maximumWidth")
        self.pa.setDuration(500)
        self.pa.setStartValue(self.zuobianlan.width())
        self.pa.setEndValue(w2)
        self.pa.start()
if __name__=='__main__':

    app=QApplication(sys.argv)
    a=Logic()
    a.show()
    sys.exit(app.exec_())